package Thread.demo6;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * 类锁和对象锁的测试
 *
 * @author
 * @create 2018-10-10 9:10
 **/
public class SynchonizedTest {

    public synchronized void function() {

        for (int i = 0; i < 4; i++) {
            System.out.println("function" + i);
        }
    }

    public static synchronized void staticFunction() {
        for (int i = 0; i < 4; i++) {
            System.out.println("staticFunction" + i);
        }
    }

    public static void main(String[] args) {

        final SynchonizedTest synchonizedTest = new SynchonizedTest();

        // 创建线程执行静态方法
        Thread t1 = new Thread(() -> {
            try {
                staticFunction();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        // 创建线程执行实例方法
        Thread t2 = new Thread(() -> {
            try {
                synchonizedTest.function();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        // 启动
        t1.start();
        t2.start();

    }
}
